package com.idenger.modules.edu.mobileWeb.utils;

import java.io.File;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.common.config.Global;
import com.idenger.common.web.BaseController;
import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.idenger.common.utils.IdGen;
import com.idenger.common.web.Servlets;
import com.idenger.modules.edu.entity.user.EduUser;



@Controller
@RequestMapping(value = "${mobilePath}/edu/utils/fileupload")
public class FileUploadController extends BaseController {
	/**
	 * 上传图片
	 * */
	@RequestMapping(value = "applyHeadPic")
	public String  applyHeadPic(EduUser eduUser,HttpServletRequest request,HttpServletResponse response, Model model){
		logger.info("\n========mobile MCamUserController applyHeadPic=======\n");
		BaseResponseMsg baseResponseMsg;
		//转型为MultipartHttpRequest
        MultipartHttpServletRequest multipartRequest  =  (MultipartHttpServletRequest) request;  
      //获得文件
        MultipartFile pic  =  multipartRequest.getFile("headPic");  
        if(pic.getOriginalFilename() ==null || "".equals(pic.getOriginalFilename())){  
            model.addAttribute("message", "上传文件不存在。");
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
			baseResponseMsg.addData("上传文件不存在");
			return renderString(response, baseResponseMsg);
        }
        String imgName = pic.getOriginalFilename();
        String suffix = imgName.substring(imgName.lastIndexOf(".")+1,imgName.length());
//        if(!"pdf".equals(suffix)){
//            model.addAttribute("message", "上传格式不正确");
//            camUser.setHeadimg(null);
//        	baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
//			baseResponseMsg.addData("上传格式不正确");
//			return renderString(response, baseResponseMsg);
//        }
        String name = IdGen.uuid();
        //客户端访问路径
        String filePath = Servlets.getRequest().getContextPath() + 
                Global.USERFILES_BASE_URL + "uploadimg" + "/" + name +"."+suffix;
        //本地保存路径
        String fileLocalPath = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL + "uploadimg" + "/" + name +"."+suffix;
        //创建目录
        File saveDirFile = new File(fileLocalPath);
        if (!saveDirFile.exists()) {
            saveDirFile.mkdirs();
        }try {
            pic.transferTo(saveDirFile);
        } catch (Exception e) {
            e.printStackTrace();
            baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
			baseResponseMsg.addData("上传失败");
			return renderString(response, baseResponseMsg);
        }
        baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(filePath);
		return renderString(response, baseResponseMsg);
	}
	
	/**
	 * 上传文件
	 * */
	@RequestMapping(value = "applyFile")
	public String  applyFile(HttpServletRequest request,HttpServletResponse response, Model model){
		logger.info("\n========mobile MCamUserController applyFile=======\n");
		BaseResponseMsg baseResponseMsg;
		//转型为MultipartHttpRequest
        MultipartHttpServletRequest multipartRequest  =  (MultipartHttpServletRequest) request;  
      //获得文件
        MultipartFile compressionFile  =  multipartRequest.getFile("file");  
        if(compressionFile.getOriginalFilename() ==null || "".equals(compressionFile.getOriginalFilename())){  
            model.addAttribute("message", "上传文件不存在。");
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
			baseResponseMsg.addData("上传文件不存在");
			return renderString(response, baseResponseMsg);
        }
        String fileName = compressionFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
        String[] str = fileName.split("\\.");
//        String name = IdGen.uuid();
        String name = str[0]+"["+new Date().getTime()+"]";
        //客户端访问路径
        String filePath = Servlets.getRequest().getContextPath() + 
                Global.USERFILES_BASE_URL + "uploadFile" + "/" + name +"."+suffix;
        //本地保存路径
        String fileLocalPath = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL + "uploadFile" + "/" + name +"."+suffix;
        //创建目录
        File saveDirFile = new File(fileLocalPath);
        if (!saveDirFile.exists()) {
            saveDirFile.mkdirs();
        }try {
            compressionFile.transferTo(saveDirFile);
        } catch (Exception e) {
            e.printStackTrace();
            baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
			baseResponseMsg.addData("上传失败");
			return renderString(response, baseResponseMsg);
        }
        baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(filePath);
		return renderString(response, baseResponseMsg);
	}
}
